#include <QtDebug>
#include <QByteArray>

#include "texmetercomm.h"

texMeterComm::texMeterComm() : QextSerialPort()
{
}

texMeterComm::texMeterComm(const QString &name) : QextSerialPort(name)
{
}

QByteArray texMeterComm::readLine(qint64 maxlen)
{
    if(!isOpen()) return NULL;

    // read until you reach a CRLF or maxlen
//    char input[2];
    char smallBuffer[2];
    char buffer[maxlen+1];
    qint64 readBytes = 0;
    do {
        readBytes += read(smallBuffer, 1);
        if(readBytes > 0)
            buffer[readBytes-1] = smallBuffer[0];
    } while(readBytes < maxlen && buffer[readBytes-1] != '\n');
    buffer[readBytes-2] = '\0';   // add null string termination, -2 to eliminate CRLF
    QByteArray incoming = QByteArray( QString(buffer).toAscii() );
//    qDebug() << " readLine() Full message: " << incoming;
    return incoming;
}
